package com.budwk.app.sqfwV2.generator.landscapePatrol;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.LandscapePatrolDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.LandscapePatrolEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class LandscapePatrolExcelGenerator extends BaseExcelGenerator {

    private String templateName;


    public LandscapePatrolExcelGenerator(){}

    public LandscapePatrolExcelGenerator(String templateName){
        this.templateName=templateName;
    }

    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user user=sysUserService.getUserById(SecurityUtil.getUserId());
        excelParam.put("province",user.getProvince());
        excelParam.put("city",user.getCity());
        excelParam.put("year", LocalDateTime.now().getYear() + "年度");
        excelParam.put("district", StrUtil.formatZhiShu(user.getDistrict()));
        excelParam.put("county", StrUtil.formatZhiShu(user.getCounty()));
        excelParam.put("community", StrUtil.formatZhiShu(user.getCommunity()));
    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        CommonParam createUserCommonParam =  rowDataParam.getCreateUserCommonParam();
        LandscapePatrolDTO landscapePatrolDTO = (LandscapePatrolDTO) rowDataParam.getAbstractCustomerPdfCreateField();
        rowDataMap.put("county", createUserCommonParam.getCounty());
        rowDataMap.put("startTime", DateUtil.format(landscapePatrolDTO.getStartTime(),"yyyy年MM月dd日HH时mm分"));
        rowDataMap.put("endTime", DateUtil.format(landscapePatrolDTO.getEndTime(),"yyyy年MM月dd日HH时mm分"));
        rowDataMap.put("fillDate", DateUtil.format(landscapePatrolDTO.getFillDate(),"yyyy年MM月dd日HH时mm分"));
        if(ObjectUtil.isNotEmpty(landscapePatrolDTO.getOfficerName())){
            rowDataMap.put("name",landscapePatrolDTO.getOfficerName().getName());
            rowDataMap.put("officerPost",landscapePatrolDTO.getOfficerName().getOfficerPost());
            rowDataMap.put("officeLevel",landscapePatrolDTO.getOfficerName().getOfficeLevel());
        }else {
            rowDataMap.put("name","");
            rowDataMap.put("officerPost","");
            rowDataMap.put("officeLevel","");
        }
        if(StringUtils.isNotEmpty(landscapePatrolDTO.getInspectionPhotos())){
            List<String> photos = Arrays.asList(landscapePatrolDTO.getInspectionPhotos().split(","));
            rowDataMap.put("visitPictures", addPictureToByteList(photos.get(0), 1));
        }else {
            rowDataMap.put("visitPictures", null);
        }
        if(ObjectUtil.isNotEmpty(landscapePatrolDTO.getRiskDescription())){
            if(StringUtils.isNotEmpty(landscapePatrolDTO.getRiskDescription().getRiskDescription())){
                rowDataMap.put("key", StrFormatter.format("{},{}",landscapePatrolDTO.getRiskDescription().getKey(),landscapePatrolDTO.getRiskDescription().getRiskDescription()) );
            }else {
                rowDataMap.put("key", StrFormatter.format("{}",landscapePatrolDTO.getRiskDescription().getKey()));
            }
        }else {
            rowDataMap.put("key", "");
        }


    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return LandscapePatrolEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
        protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {
            excelFileCreateParamBO.setImgWidth(180);
            excelFileCreateParamBO.setRowHeight(122);
        }


}
